/**
 * Created by gongxun on 17/1/13.
 */

var PdkCardHelper = function(){
    var that = {};

    that.design = cc.size(151, 209);

    that.mInhandCardScale = null;

    that.mInhandCardPointY = null;

    that.mDealCardScale = null;

    that.cardLeftOffset = 30;

    that.leftOffset = 0;

    that.maxInhandOffset = 100;

    that.minInhandOffset = 30;

    //时钟和出牌的位置
    that.clockPos = null;

    that.init = function(){
        var maxLen = 16;
        // var temp = that.cardLeftOffset * 2 + (maxLen - 1) * that.leftOffset;
        // that.mInhandCardScale = (mo.gameSize.x - temp) / maxLen / that.design.width;
        that.mInhandCardScale = 0.8;
        that.mDealCardScale = 0.5;
        that.mInhandCardPointY = 0;
    };

    that.initClockPos = function(node){
        that.clockPos = [];
        for (var i = 0; i < 3; i++){
            var pos = mo.seekWidgetByName(node, "Panel_playCard" + i).getPosition();
            that.clockPos.push(pos);
        }
    };

    that.getCardSize = function(){
        var size = cc.size(that.design.width * that.mInhandCardScale, that.design.height * that.mInhandCardScale);
        return size;
    };

    that.getDealSize = function () {
        var size = cc.size(that.design.width * that.mDealCardScale, that.design.height * that.mDealCardScale);
        return size;
    };

    that.getExistCardNum = function (cardNum) {
        if (cardNum >= 1 && cardNum <= 52){
            return true;
        }
        return false;
    };

    that.getRandomCard = function(size){
        var result = [];
        for (var i = 0; i < size; i++){
            result.push(parseInt(Math.random() * 52) + 1);
        }
        return result;
    };

    that.getInhandCardScale = function(seat){
        var result = that.mInhandCardScale;
        return result;
    };

    that.getInhandCardPos = function(seat, index, count){
        var pos = cc.p(0, 0);
        var center = cc.p(0, 0);
        var cardSize = that.getCardSize();
        var scale = 1;
        if (seat == 0){

            if (count <= 1){
                pos.x = mo.gameSize.x / 2;
                pos.y = that.getInhandCardHeight();
            }else{
                var offset = 0;
                offset = (mo.gameSize.x - that.cardLeftOffset * 2 - cardSize.width) / (count - 1);
                offset = Math.min(offset, that.maxInhandOffset);
                offset = Math.max(offset, that.minInhandOffset);

                var xStart = mo.gameSize.x / 2 - (cardSize.width + (count - 1) * offset) / 2 + cardSize.width / 2;
                pos.x = xStart + offset * index;
                pos.y = that.getInhandCardHeight();
            }

        }else if (seat == 1){

        }else if (seat == 2){

        }

        return pos;
    };

    that.getDealCardPos = function(seat, index, count){
        var pos = cc.p(0, 0);
        var center = that.clockPos[seat];
        var offset = 30;
        var cardSize = that.getDealSize();

        //基础位置
        var indexYushu = index % 8;
        var countYushu = count > 8 ? 8 : count;
        if (seat == 0){
            var startX = center.x - ((countYushu - 1) * offset + cardSize.width) / 2 + cardSize.width / 2;
            pos.x = startX + indexYushu * offset;
            pos.y = center.y;
        }else if (seat == 1){
            var startX = center.x - (countYushu - 1) * offset - cardSize.width + cardSize.width / 2;
            pos.x = startX + indexYushu * offset;
            pos.y = center.y;
        }else if (seat == 2){
            var startX = center.x + cardSize.width / 2;
            pos.x = startX + indexYushu * offset;
            pos.y = center.y;
        }

        //超过位置后变化
        if (count > 8){
               if (index < 8){
                   pos.y += 35;
               }else{
                   pos.y -= 25;
               }
        }

        return pos;
    };

    that.getDealCardCenterPos = function(seat, count){
        var tempCount = count;
        if (count > 8){
            tempCount = 8;
        }
        var isSing = tempCount % 2 == 1;
        var pos = that.getDealCardPos(seat, parseInt(tempCount / 2), tempCount);
        pos.y += -that.getDealSize().width / 2;
        var offsetX = 0;
        if (!isSing){
            offsetX -= 15;
        }
        pos.x += offsetX;

        return pos;
    };

    that.getShowCardScale = function(seat){
        var expectedSize = that.design.width * 0.78 / that.getCardSize().width;
        return expectedSize;
    };

    that.getShowCardPos = function(seat, index, cardType){
        var result = cc.p(0, 0);
        if (seat == 0){
            var pos = cc.p(0, 0);
            var center = cc.p(mo.gameSize.x / 2, 100);
            var cardSize = that.getCardSize();
            var scale = 0.78;
            var addX = 45;

            pos.y = center.y;
            pos.x = (index - 2) * addX * scale + center.x;
            result = pos;
        }else{
            result = that.getInhandCardPos(seat, index);
        }

        //如果是开的有牛的 要留出间隔
        if (cardType > 0 && cardType <= 10){
            if (seat == 1 || seat == 2){
                if (index < 3){
                    result.x -= 20;
                }
            }else if (seat == 3 || seat == 4){
                if (index > 2){
                    result.x += 20;
                }
            }else{
                if (index < 3){
                    result.x -= 8;
                }else{
                    result.x += 12;
                }
            }
        }

        return result;
    };

    //返回表情序列帧
    that.getFaceFrames = function (index) {
        var spriteFrames = [];
        for (var i = 0; i <= 60; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame("emoji_" + index + "_" + i + ".png");
            if (frame == null){
                break;
            }
            spriteFrames.push(frame);
        }
        return spriteFrames;
    };

    //返回牌的高
    that.getInhandCardHeight = function(){
        return that.getCardSize().height / 2 + that.mInhandCardPointY;
    };

    //手牌排序从大到小
    that.sortCard = function(nums){
        nums.sort(function(a, b){
            var aZhi = mo.pdkCardRule.getCardNumMax(a).zhi;
            var bZhi = mo.pdkCardRule.getCardNumMax(b).zhi;
            if (aZhi == bZhi){
                return a - b;
            }else{
                return bZhi - aZhi;
            }
        });
    };

    //手牌排序 从小到大
    that.sortCardEx = function(nums){
        nums.sort(function(a, b){
            var aZhi = mo.pdkCardRule.getCardNumMax(a).zhi;
            var bZhi = mo.pdkCardRule.getCardNumMax(b).zhi;
            if (aZhi == bZhi){
                return a - b;
            }else{
                return aZhi - bZhi;
            }
        });
    };

    return that;
};

mo.pdkCardHelper = new PdkCardHelper();